/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.userManagement.bean.TaskSessionBean;
import Merlion.userManagement.bean.TaskSessionBeanRemote;
import Merlion.userManagement.entity.TaskEntity;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;


@ManagedBean(name = "TaskManagedBean")
@ViewScoped
public class TaskManagedBean implements Serializable {

    private TaskEntity newTask = new TaskEntity();
    private TaskEntity selectedTask = new TaskEntity();
    private TaskEntity selectedViewTask;
    private List<TaskEntity> taskList;
    private String title;
    private String task;
    private Date dateDue;
    private int h;
    private int m;
    private String reminder;
    private int rTime;
    private boolean editMode;

    /** Creates a new instance of TaskManagedBean */
    public TaskManagedBean() {
    }
    @EJB
    private TaskSessionBeanRemote tsb;

    public Date getDateDue() {
        return dateDue;
    }

    public void setDateDue(Date dateDue) {
        this.dateDue = dateDue;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getM() {
        return m;
    }

    public void setM(int m) {
        this.m = m;
    }

    public int getrTime() {
        return rTime;
    }

    public void setrTime(int rTime) {
        this.rTime = rTime;
    }

    public String getReminder() {
        return reminder;
    }

    public void setReminder(String reminder) {
        this.reminder = reminder;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void checkReminder() {
        List<TaskEntity> list = tsb.checkReminder();
        if (list != null && !list.isEmpty()) {
 
            TaskEntity t = list.get(0);

            String eTitle = t.getTitle();
            System.out.println("task is " + eTitle);

            int reminderTime = t.getReminderTime();
            System.out.println("Event not null");
    
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Event Reminder", "The following task : (Name: " + eTitle + ") is about to be expired in " + reminderTime + " minutes!"));

        }
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (taskList == null) {
            taskList = tsb.viewTask();
        }
    }

    public TaskEntity getNewTask() {
        return newTask;
    }

    public void setNewTask(TaskEntity newTask) {
        this.newTask = newTask;
    }

    public List<TaskEntity> getTaskList() {
        if (taskList == null) {
            taskList = tsb.viewTask();
        }
        return taskList;
    }

    public void setTaskList(List<TaskEntity> taskList) {
        this.taskList = taskList;
    }

    public TaskEntity getSelectedTask() {
        return selectedTask;
    }

    public void setSelectedTask(TaskEntity selectedTask) {
        this.selectedTask = selectedTask;
    }

    public TaskEntity getSelectedViewTask() {
        return selectedViewTask;
    }

    public void setSelectedViewTask(TaskEntity selectedViewTask) {
        this.selectedViewTask = selectedViewTask;
    }

    public void addTask(ActionEvent event) {
        System.out.println("add Task :: WEB");

        boolean reminder1 = false;
        if (reminder.equals("Y")) {
            reminder1 = true;
        }

        System.out.println("Call add task from ejb");
        tsb.addTask(title, task, dateDue, h, m, 0, reminder1, rTime);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("tasks.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void deleteTask(ActionEvent event) {

        if (selectedTask != null) {
            tsb.deleteTask(selectedTask.getId());
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("tasks.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void save(ActionEvent event) {

        if (selectedViewTask != null) {
            String eTitle = selectedViewTask.getTitle();
            String etask = selectedViewTask.getTask();
            Date edateDue = selectedViewTask.getDateDue();
            int ehourDue = selectedViewTask.getHourDue();
            int eminDue = selectedViewTask.getMinuteDue();
            int esecondDue = selectedViewTask.getSecondDue();
            boolean ereminder = selectedViewTask.isReminder();
            int ereminderTime = selectedViewTask.getReminderTime();
            Long eid = selectedViewTask.getId();

            tsb.editTask(eid, eTitle, etask, edateDue, ehourDue, eminDue, 0, ereminder, ereminderTime);

        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("tasks.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
